<?php
class messageController extends userController{
    
    public function index() {
        //分页参数
        $url=url('message/index',array('page'=>'{page}','type'=>intval($_GET['tpye'])));
        $limit = $this->pageLimit($url, 20);
        //获取条件
        $type=intval($_GET['type']);
        switch ($type) {
            case 1:
                $where='B.uid='.$this->user_id.' AND A.system=1';
                $table='receive';
                break;
            case 2:
                $where='B.uid='.$this->user_id;
                $table='send';
                break;
            default:
                $where='B.uid='.$this->user_id;
                $table='receive';
                break;
        }
        //列表信息
        $this->list=model('message')->message_list($limit,$where,$table);
        $count=model('message')->count($where,$table);
        $this->page=$this->pageShow($count);
        $this->show();  
    }

    //详细信息
    public function info() {
        $mid=$_GET['mid'];
        if(empty($mid)){
            $this->errorpage('短信息不存在或者无权限访问');
        }
        $this->info=model('message')->info($mid,'receive');
        if($this->info['uid']<>$this->user_id){
            $this->errorpage('短信息不存在或者无权限访问');
        }
        $this->user=model('user_list')->info($this->info['send_uid']);

        $data=array();
        $data['mid']=$this->info['mid'];
        $data['uid']=$this->user_id;
        $data['view']=1;
        model('message')->view($data);
        $this->show(); 
    }

    //添加
    public function add() {
        $this->show(); 
    }

    public function add_save() {
        $data=in($_POST);
        //查找用户信息
        $user=model('user_list')->user_info($data['username']);
        if(!$user){
            $this->msg('收件人填写错误，请重新填写！',0);
        }
        if($user['uid']==$this->user_id){
            $this->msg('您不能给自己发送信息！',0);
        }
        $data['receive_uid']=$user['uid'];
        $data['send_uid']=$this->user_id;
        $data['title']=text_in($data['title']);
        $data['content']=text_in($data['content']);
        $data['time']=time();
        $data['ip']=get_client_ip();
        model('message')->add($data);
        $this->msg('短信息发送成功！',1);
    }

    //删除
    public function batch() {
        $id_array=substr($_POST['mid'],0,-1);
        $id_array=explode(',', $id_array);
        $type=intval($_POST['type']);
        if(!isset($type)||empty($id_array)){
            $this->msg('参数传递错误！',1);
        }
        switch ($type) {
            case 2:
                $action='send_del';
                $table='send';
                break;
            default:
                $action='receive_del';
                $table='receive';
                break;
        }
        foreach ($id_array as $value) {
            $info=model('message')->info($value,$table);
            if($info['uid']<>$this->user_id){
                $this->msg('您无法删除该条信息！',0);
            }
            model('message')->$action($value,$this->user_id);
        }
        $this->msg('短信息删除成功！',1);
        
    }

    //获取未读消息数量
    public function unread_num() {
        $where='B.uid='.$this->user_id. ' AND B.view<>1';
        $table='receive';
        echo model('message')->count($where,$table);
    }

}